#include "StdAfx.h"
#include "MomentFeature.h"
#include "cv.h"
#include "highgui.h"

CMomentFeature::CMomentFeature(void)
{
}

CMomentFeature::~CMomentFeature(void)
{
}

BOOL CMomentFeature::Calc(CString& sPath, CArray<double>& arrFeatures)
{
	IplImage* img = NULL;
	char arrFileName[MAX_PATH];
	CFeatureExtract::CString2Char(sPath, arrFileName);
	img = cvvLoadImage(arrFileName);

	if(img)
	{
		CvMoments moment;
		CvHuMoments huMoment;
		IplImage* imgHSV, *imgH, *imgS, *imgV;
		imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
		imgH = cvCreateImage(cvGetSize(img), 8, 1);
		imgS = cvCreateImage(cvGetSize(img), 8, 1);
		imgV = cvCreateImage(cvGetSize(img), 8, 1);
		cvCvtColor(img, imgHSV, CV_RGB2HSV);
		cvCvtPixToPlane(imgHSV, imgH, imgS, imgV, NULL);

		cvMoments(imgV, &moment);
		cvGetHuMoments(&moment, &huMoment);

		cvReleaseImage(&imgH);
		cvReleaseImage(&imgS);
		cvReleaseImage(&imgV);
		cvReleaseImage(&imgHSV);
		cvReleaseImage(&img);

		arrFeatures.RemoveAll();
		arrFeatures.Add(huMoment.hu1);
		arrFeatures.Add(huMoment.hu2);
		arrFeatures.Add(huMoment.hu3);
		arrFeatures.Add(huMoment.hu4);
		arrFeatures.Add(huMoment.hu5);
		arrFeatures.Add(huMoment.hu6);
		arrFeatures.Add(huMoment.hu7);
		return TRUE;
	}
	return FALSE;
}